package it.polimi.swKnights.SWIMv2.general;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class ErrorTagHandler extends TagSupport
{
	private String error;

	private static final long serialVersionUID = -1664487562666025464L;

	@Override
	public int doStartTag() throws JspException
	{
		try
		{
			// Get the writer object for output.
			JspWriter out = this.pageContext.getOut();
			String inTheHtml = "";
			
			if(this.error != null && !this.error.equals("")){
				inTheHtml = "<p>" + this.getErrorMessage() + "</p>";
			} else {
				return SKIP_BODY;
			}

			out.println(inTheHtml);

		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		return SKIP_BODY;
	}

	private String getErrorMessage()
	{
		try
		{	
			Errors error = Errors.valueOf(this.error);
			return "Error: " + error.getErrorMessage();
		}
		catch (IllegalArgumentException e)
		{
			return "Unknown Error: please contact an admin to report this strange situation";
		}
	}

	/*
	 * Getters and setters
	 */

	public String getError()
	{
		return error;
	}

	public void setError(String error)
	{
		this.error = error;
	}

}
